<?php
/**
 * 	PHP CLOUND LIBRARY
 *	Lớp đối tượng tạo code html mô tả đối tượng trên form
 *****************************************************
 */
require_once 'Zend/Form.php';
class Clound_Xml_GenerateObject extends Zend_Form
{
	private $_decoratorPath = array('decorator' => array('Clound_Xml_Decorator' => 'Clound/Xml/Decorator/'));
	public function _formfieldGenerate($arrStruct,$arrObject,$arrform_option,$arrValue=null,$arrXmlData=null)
    {
		//Ghép mảng giá trị
		if(isset($arrXmlData)){
			$arrValue = array_merge($arrValue,$arrXmlData);
		}
		//Set forrm
		$this->setAttribs($arrform_option['option']);
		$this->setDecorators(array('FormElements',array('HtmlTag', array('tag' => 'DIV', 'class' => 'frm')),'Form'));
		//Duyệt từng group
		$objStruct = new ArrayObject($arrStruct['group']);
		$objObject = new ArrayObject($arrObject);
		$iterator = $objStruct->getIterator();
		while($iterator->valid()){
			$arrRow = $iterator->current();
			$sTag = $arrRow['tag_list'];
			$arrGroupE = explode(',',$sTag);
			$arrGroupTemp = array();
			foreach ($arrGroupE as $stemp) {
				$arrEoption = $objObject->offsetGet($stemp);
				$arrOption = $arrEoption['options'];
				//Lay gia trị
				if((isset($arrOption['data_store']))&&(isset($arrValue))){
					if(isset($arrValue[$arrOption['data_store']])){
						$arrOption['value'] = $arrValue[$arrOption['data_store']];
					}
				}
				if(isset($arrEoption['multi'])){
					if(isset($arrOption['value'])){
						$arrOption['value'] = explode(',',$arrOption['value']);
					}
					$arrOption['multioptions']=$this->_getMultiData($arrEoption['input_data']);
				}
				if(isset($arrEoption['decorators'])){
					if($arrEoption['type']==='file'){
						$svalue = '';
						if(isset($arrOption['value'])){
							$svalue = $arrOption['value'];
						}
						$arrOption = array(
											'disableLoadDefaultDecorators' => true,
											'decorators' => array(
												'Label',
												'File',
												array('ViewScript', array(
												'viewScript' => 'decorators/file.phtml',
												'placement' => false,
												))
											),
											'fileid' => $svalue,
										);
					}else{
						$arrOption['prefixPath'] = $this->_decoratorPath;
						$arrOption['decorators']=array($arrEoption['decorators']);
					}
				}
				$element = $this->createElement($arrEoption['type'],$arrEoption['id'],$arrOption);
				$this->addElements(array($element));
				array_push($arrGroupTemp,$arrEoption['id']);
			}
			if(isset($arrRow['id'])){
				$sGroupId = $arrRow['id'];
				$sGroupClass = '';
				if(isset($arrRow['class'])){
					$sGroupClass = $arrRow['class'];
				}
				$arrRowOption = array();
				if(isset($arrRow['options'])){
					$arrRowOption = $arrRow['options'];
				}
				$this->addDisplayGroup($arrGroupTemp,$sGroupId,$arrRowOption);
				if(isset($arrRowOption['legend'])){
					$arrDecorater = array(
					'FormElements',
					'Fieldset',
					array('HtmlTag',
						array('tag'     => 'div',
							 'class'    => "block $sGroupClass"
							 )
						 )
					);
				}else{
					$arrDecorater = array(
					'FormElements',
					array('HtmlTag',
						array('tag'     => 'div',
							 'class'    => "block $sGroupClass"
							 )
						 )
					);
				}
				$objGroup = $this->getDisplayGroup($sGroupId);
				$objGroup->setDecorators($arrDecorater);
			}
			$iterator->next();
		}
        return $this;
    }
	/**
	*	Lấy mảng giá trị cho đối tượng Multi
	*
	**/
	private function _getMultiData($arrInput_data)
	{
		$arrObject = array();
		$sDataType = $arrInput_data['type'];
		switch ($sDataType){
			//Trường hợp lấy trong session
			case "session":
				$app = Zend_Registry::get('App');
				$projectname = $app->name;
				$sSessionName = $arrInput_data['code'];
				$arrInput = $_SESSION[$projectname][$sSessionName];
				foreach ($arrInput as $arrTemp){
					$arrObject[$arrTemp[$arrInput_data['value']]] = $arrTemp[$arrInput_data['name']];
				}
				break;
			//Trường hợp lấy trong danh mục
			case "list":
				$sListCode = $arrInput_data['code'];
				$cacheFile = new Clound_CacheFile();
				$arrInput = $cacheFile->getList($sListCode);
				foreach ($arrInput as $arrTemp){
					$arrObject[$arrTemp[$arrInput_data['value']]] = $arrTemp[$arrInput_data['name']];
				}
				break;
		}
		return $arrObject;
	}
}
